//入口文件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
//定义全局过滤器
import router from './router.js'
import Resource from 'vue-resource'
Vue.use(Resource)
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
//获取本地存储数据
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    addToCar(state, goodsinfo) {
      var flag = false
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count += parseInt(goodsinfo.count)
          flag = true
          return true
        }
      })
      if (!flag) {
        state.car.push(goodsinfo)
      }
      //本地存储car数组
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsInfo(state, goodsinfo) {
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    removeFromCar(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsSelected(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {
    getAllCount(state) {
      var c = 0
      state.car.forEach(item => {
        c += item.count
      })
      return c
    },
    getGoodsCount(state) {
      var obj = {}
      state.car.forEach(item => {
        obj[item.id] = item.count
      })
      return obj
    },
    getGoodsSelected(state) {
      var select = {}
      state.car.forEach(item => {
        select[item.id] = item.selected
      })
      return select
    },
    getGoodsCountAndAmount(state) {
      var obj = {
        count: 0,
        amount: 0
      }
      state.car.forEach(item => {
        if (item.selected) {
          obj.count += item.count
          obj.amount += item.price * item.count
        }
      })
      return obj
    }
  }
})

import {
  Header,
  Tabbar,
  TabItem,
  Swipe,
  SwipeItem,
  Button,
  Lazyload,
  Switch
} from 'mint-ui'

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import 'bootstrap/dist/css/bootstrap.css'
import './public/css/home.css'

import app from './App.vue'
Vue.component(Header.name, Header)
Vue.component(Tabbar.name, Tabbar)
Vue.component(TabItem.name, TabItem)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button)
Vue.component(Switch.name, Switch)
Vue.use(Lazyload)

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router, //挂载路由对象
  store //管理状态对象
})